﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;

namespace GeneratorLibrary.Client.Base {
    public abstract class BaseDAC : IDisposable {
        protected DbConnection conn;
        protected string schema;

        private BaseDAC() { }
        public BaseDAC(DbConnection connection, string schema) {
            this.conn = connection;
            this.schema = schema;
            connection.Open();
        }
        #region IDisposable Members

        public void Dispose() {
            if (conn.State != ConnectionState.Closed) {
                conn.Close();
            }
        }

        #endregion

        public abstract void Execute(string sql, DbParameter[] parameters);
        public abstract DataTable GetColumns(string tableOrViewLike);
        public abstract List<string> GetDatabases();
        public abstract string GetInserts(string tableLike, bool disableIdentity);
        public abstract List<string> GetTables();
        public abstract List<string> GetViews();
        public abstract List<string> GetProcs();
        public abstract List<string> GetProcs(string procLike);

        protected abstract DbCommand NewCommand(string strSql, DbParameter[] parameters);

        protected List<string> GetList(string strSql, DbParameter[] parameters) {
            List<string> list = new List<string>();
            DbCommand command = NewCommand(strSql, parameters);
            using (IDataReader reader = command.ExecuteReader()) {
                while (reader.Read()) {
                    list.Add(reader.GetString(0));
                }
            }
            return list;
        }
    }
}
